var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;

    //工单产品状态
    var stateTemp = $("#stateTemp").val();
    if (stateTemp != null && stateTemp != "" && stateTemp != undefined) {
        $("#jpState").val(stateTemp);
        form.render();
    }

    //关闭
    var closed = $(".close");
    closed.click(function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index); //再执行关闭
    });
    //监听提交
    form.on('submit(submit)', function () {
        //提交按钮置为不可用
        $("#submit").addClass("layui-btn-disabled");
        addOrUpdateJobProduct();
    });
});


/**
 * 新增/修改
 */
function addOrUpdateJobProduct() {
    var jpId = $.trim($("#jpId").val());
    //工单ID
    var jobId = $.trim($("#jobId").val());
    // 产品编号
    var productCode = $.trim($("#productCode").val());
    // 客户Id
    var problem = $("#problem").val();
    //处理人
    var dealId = $("#userId").val();
    //处理动作
    var jpState = $("#jpState").val();
    //解决方案
    var solution = $("#solution").val();
    // 备注
    var remark = $("#remark").val();

    var type = $("#type").val();

    if (null == type || '' == type) {
        type = 'add';
        jobId = $.trim($("#jobIdParam").val());
    }

    var paramData = {
        "jobProduct.id": jpId,
        "jobProduct.jobId": jobId,
        "jobProduct.productCode": productCode,
        "jobProduct.problem": problem,
        "jobProduct.dealId": dealId,
        "jobProduct.state": jpState,
        "jobProduct.solution": solution,
        "jobProduct.remark": remark,
        "type": type
    };
    $.ajax({
        type: 'post',
        url: basePath + "job/saveJobProduct.action",
        data: paramData,
        dataType: 'text',
        success: function (data) {
            if (data != null && data != "" && data == "success") {
                closeAndRefresh();
            } else {
                var content = '';
                if (type == "add") {
                    content = '新增失败!';
                } else {
                    content = '修改失败!';
                }
                layer.open({
                    title: '错误',
                    icon: 5,
                    content: content,
                    yes: function () {
                        closeAndRefresh();
                    }
                });
            }
            //提交按钮置为可用
            $("#submit").removeClass("layui-btn-disabled");
        }
    });
}
